/*
 * Copyright 2018 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.netflix.zuul.sample.filters.inbound

import com.netflix.zuul.context.SessionContext
import com.netflix.zuul.filters.http.HttpInboundSyncFilter
import com.netflix.zuul.message.HeaderName
import com.netflix.zuul.message.Headers
import com.netflix.zuul.message.http.HttpQueryParams
import com.netflix.zuul.message.http.HttpRequestMessage
import com.netflix.zuul.message.http.HttpRequestMessageImpl
import com.netflix.zuul.netty.filter.ZuulEndPointRunner
import com.netflix.zuul.sample.filters.endpoint.Healthcheck
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * Routes configuration
 *
 * Author: Arthur Gonigberg
 * Date: November 21, 2017
 */
class Routes extends HttpInboundSyncFilter {
    private static final Logger log = LoggerFactory.getLogger(Routes.class)

    @Override
    int filterOrder() {
        return 0
    }

    @Override
    boolean shouldFilter(HttpRequestMessage httpRequestMessage) {
        return true
    }

    @Override
    HttpRequestMessage apply(HttpRequestMessage request) {
        SessionContext context = request.getContext()
        String path = request.getPath()
        String host = request.getOriginalHost()



        log.info("path:{}",path)
        log.info("host:{}",host)
        log.info("request:{}",request.toString())
        /*HttpQueryParams queryParams = request.getQueryParams()
        queryParams.add("name", "forezp")
        Headers headers = request.getHeaders()
        headers.remove("Host")
        headers.add("Host", "127.0.0.1:8762")
        String proto = request.getProtocol()
        String method = request.getMethod()
        String pathOld = "/hi"
        String clientIp = request.getClientIp()
        String scheme = request.getScheme()
        int port = 8762
        String serverName = request.getServerName()

        request = new HttpRequestMessageImpl(context,proto, method, pathOld, queryParams, headers, clientIp, scheme, port, serverName)



        request.reconstructURI()
        request.storeInboundRequest()*/
        log.info("request:{}",request.toString())
//        context.setEndpoint(Healthcheck.class.getCanonicalName())

        // Route healthchecks to the healthcheck endpoint.;
        if (path.equalsIgnoreCase("/healthcheck")) {
            context.setEndpoint(Healthcheck.class.getCanonicalName())
        }
        else {
            context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME)
            context.setDebugRouting(true)
//            context.setRouteVIP("api")
        }


        return request
    }
}
